var addTwoNumbers = function(l1, l2) {
    debugger;
    let head = null, tail = null;
    let carry = 0;
    while (l1 || l2) {
        const n1 = l1 ? l1.val : 0;
        const n2 = l2 ? l2.val : 0;
        const sum = n1 + n2 + carry;
        if (!head) {
            head = tail = new ListNode(sum % 10);
        } else {
            tail.next = new ListNode(sum % 10);
            tail = tail.next;
        }
        carry = Math.floor(sum / 10);
        if (l1) {
            l1 = l1.next;
        }
        if (l2) {
            l2 = l2.next;
        }
    }
    if (carry > 0) {
        tail.next = new ListNode(carry);
    }
    return head;
};

function ListNode(val, next) {
    this.val = (val===undefined ? 0 : val)
    this.next = (next===undefined ? null : next)
}

// 从头部增加
function addFirst(link,val){
    const node=new ListNode();
    node.val=val;
    node.next=link;
    return node;
 }

function test(l1_arr,l2_arr){
    console.log("hi");
    let l1=null;
    for (let index = l1_arr.length-1; index >= 0; index--) {
       l1=addFirst(l1,l1_arr[index]);
    }
 let l2=null;
 for (let index = l2_arr.length-1; index >= 0; index--) {
    const element = l2_arr[index];
    
    l2=addFirst(l2,l2_arr[index]);
    
 }
  
    addTwoNumbers(l1,l2);
 }

 test([1,2,3],[4,5,6])